package com.ctgu.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.entity.Account;
import com.ctgu.mapper.AccountMapper;
import com.ctgu.service.AccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author nepture
 */
@Service
public class AccountImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Resource
    AccountMapper accountMapper;

    /**
     * todo: 检验账号密码是否输入成功
     *
     * @param telephone 手机号
     * @param pwd       密码
     * @return 返回该账号是否存在
     */
    public boolean checkPwdAndAccount(String telephone, String pwd) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone", telephone).eq("pwd", pwd);
        List<Account> accounts = accountMapper.selectList(queryWrapper);
        return accounts.size() != 0;
    }

    /**
     * todo: 注册账号
     *
     * @param telephone 手机号
     * @param pwd       密码
     * @return 返回该账号是否注册成功
     */
    public boolean addAccount(String telephone, String pwd) {
        Account account = new Account();
        account.setPwd(pwd);
        account.setTelephone(telephone);
        return accountMapper.insert(account) > 0;
    }

    /**
     * todo: 修改账户密码
     *
     * @param telephone 手机号
     * @param pwd       密码
     * @return 返回该账号密码是否修改成功
     */
    public boolean updateAccount(String telephone, String pwd) {
        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("telephone", telephone).set("pwd", pwd);
        return accountMapper.update(null, updateWrapper) > 0;
    }

    /**
     * todo: 删除账号
     *
     * @param telephone 手机号
     * @return 返回该账号是否删除成功
     */
    public boolean deleteAccount(String telephone) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone", telephone);
        return accountMapper.delete(queryWrapper) > 0;
    }
}
